package com.carreirati.evaluation.domain;

import java.util.Collection;
import java.util.HashSet;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "user_login", schema = "dbo")
@RooDbManaged(automaticallyDelete = true)
public class UserLogin {

	private static final long serialVersionUID = 1L;
	public String getAuthorities() {
		return this.getPermissionId().getPermission();
	}
	
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.getUsers().getFirstName() + " " + this.getUsers().getSurName();	
	}
	
	public static UserLogin findUserLoginByEmail(String email) {
        UserLogin ul = null;
        try{
        	ul = (UserLogin) entityManager().createNativeQuery("SELECT * FROM User_Login  WHERE user_id in (select id from users WHERE email='"+email  +"')", UserLogin.class).getSingleResult();
        } catch(Exception e){
        	ul = null;
        }
        return ul;
    }
	
	@Transactional
    public void persist() {
		  if (this.entityManager == null) this.entityManager = entityManager();
		// if (this.entityManager == null) this.entityManager = entityManager();
		  String iiAux = "";
		  String aux ="";
		  if (this.getPassword().length()>=0){
			  iiAux = ",password";
			  aux = "'" + this.getPassword() + "',";
		  }
	        String sqlString = "INSERT INTO user_login (user_id" + iiAux +", permission_id, last_login, enabled) ";
	        sqlString += " VALUES('" + this.getUsers().getId() +"',"+ aux +"'" + this.getPermissionId().getId()+ "',null,'"+ this.getEnabled() +"')";
	        this.entityManager.createNativeQuery(sqlString).executeUpdate();
    }
	
	 @Transactional
	    public UserLogin merge() {		  
		  String aux ="";
		  String permissionAux = "";
		  if (this.getPassword().length()>=0){
			  aux = ",password = '" + this.getPassword() + "'";			  
		  }
		  
		  if( this.getPermissionId().getId() == null)
			  permissionAux = " permission_id=permission_id";
		  else
			  permissionAux = " permission_id="+ this.getPermissionId().getId() + "";
		  String sqlString = "UPDATE user_login set " +permissionAux +",";
		  sqlString += " enabled = '" + this.getEnabled() + "' " + aux ;
		  sqlString += " WHERE user_id='" +  this.getUsers().getId() + "'";

	      this.entityManager.createNativeQuery(sqlString).executeUpdate();
	      return UserLogin.findUserLogin( this.getUsers().getId());
	     
	    }
	    
}
